package com.example.advice;


import com.example.exception.MyException;
import com.example.utils.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class CommonExceptionHandle {

    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerExceptionHandler(NullPointerException e){
        e.printStackTrace(); //打印异常堆栈信息
        return new Result("444","空指针错误");
    }

    @ExceptionHandler(ArithmeticException.class)
    public Result arithmeticExceptionHandler(ArithmeticException e){
        e.printStackTrace();
        return new Result("445","算术异常");
    }

    @ExceptionHandler(MyException.class)
    public Result myExceptionHandler(MyException e){
        e.printStackTrace();
        return new Result(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e){
        e.printStackTrace();
        return new Result("440","未知异常");
    }

     /*@ExceptionHandler(NullPointerException.class)
    public Result nullPointerExceptionHandler(NullPointerException ex){
        ex.printStackTrace(); //异常堆栈信息
        return new Result("555","空指针错误");
    }


    @ExceptionHandler(MyException.class)
    public Result myExceptionHandler(MyException ex){
        ex.printStackTrace(); //异常堆栈信息
        return new Result(ex.getCode(),ex.getMessage());
    }


    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception ex){
        ex.printStackTrace(); //异常堆栈信息
        return new Result("444","未知异常");
    }*/
}
